package com.yaowk.web.mall.clothing.controller;

import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.api.mall.clothing.model.Goods;
import com.yaowk.api.mall.clothing.model.GoodsImage;
import com.yaowk.api.mall.clothing.model.Specification;
import com.yaowk.api.mall.clothing.service.IGoodsService;
import com.yaowk.api.mall.clothing.service.ISpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mall/clothing/goods")
@Api(tags = "商品控制器")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISpecificationService specificationService;

    @GetMapping
    @ApiOperation("商品列表")
    public PageResp<Goods> list(Byte status, String keyword, PageReq pageReq) {
        PageResp<Goods> pageResp = goodsService.findAll(status, keyword, pageReq);
        return pageResp;
    }

    @PostMapping
    @ApiOperation("保存商品")
    public Goods save(Goods goods) {
        goods = goodsService.save(goods);
        return goods;
    }

    @PostMapping("/images")
    @ApiOperation("保存商品图片")
    public void saveImages(GoodsImage goodsImage) {
        goodsService.saveImage(goodsImage);
    }

    public void saveSpecification(Specification specification) {
        specificationService.save(specification);
    }

    @PutMapping("/delete/{id}")
    @ApiOperation("放入回收站")
    public void delete(@PathVariable Integer id) {
        goodsService.recovery(id);
    }

    @PutMapping("/down/{id}")
    @ApiOperation("下架")
    public void down(@PathVariable Integer id) {
        goodsService.down(id);
    }

    @PutMapping("/up/{id}")
    @ApiOperation("上架")
    public void up(@PathVariable Integer id) {
        goodsService.up(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public void realDelete(@PathVariable Integer id) {
        goodsService.deleteById(id);
    }
}
